package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientVisitService;
import com.naiterui.ehp.bs.patient.vo.visit.DrVisitRecomVO;
import com.naiterui.ehp.bs.patient.vo.visit.PatientVisitRecordVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用药随访
 */
@Api(tags = SwaggerTag.PATIENT + SwaggerTag.FOLLOW_UP)
@RequestMapping("ap/visit")
@Controller
public class ApPatientVisitController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApPatientVisitController.class);

    @Autowired
    private IPatientVisitService patientVisitService;

    /**
     * 获取随访记录信息
     *
     * @param visitId
     *
     * @return
     *
     * @History 2016年11月11日 下午2:28:42 by wangchuan
     */
    @RequestMapping("details")
    @ResponseBody
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public PatientVisitRecordVO getVisitRecordDetails(Long visitId, Long patientId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("获取随访记录详情，参数：patientId:{};visitId:{}", patientId, visitId);
        if (patientId == null || visitId == null) {
            LOGGER.info("参数为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return this.patientVisitService.getVisitDetailsById(patientId, visitId);
    }

    /**
     * 填写随访
     *
     * @return
     *
     * @History 2016年11月11日 下午2:32:46 by wangchuan
     */
    @RequestMapping("send")
    @ResponseBody
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public void sendVisit(Long patientId, Long visitId, String visitInfo) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("患者提交随访记录，参数：patientId:{};visitId:{};visitInfo:{}", patientId, visitId, visitInfo);
        if (patientId == null || visitId == null || StringUtils.isBlank(visitInfo)) {
            LOGGER.error("患者提交随访记录，参数错误");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        // 1. 保存随访信息
        this.patientVisitService.saveVisitInfo(visitId, visitInfo, patientId);

    }

    /**
     * 商品详情
     *
     * @param orderId
     *
     * @return
     *
     * @History 2016年11月16日 上午10:34:20 by wangchuan
     */
    @RequestMapping("goodsDetails")
    @ResponseBody
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    // 入口已屏蔽，H5端仍在调用
    @Deprecated
    public DrVisitRecomVO getVisitGoodsDetailsInOrder(Long patientId, Long orderId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("获取订单中商品详情，参数：patientId:{};orderId:{}", patientId, orderId);
        if (patientId == null || orderId == null) {
            LOGGER.error("参数为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        return this.patientVisitService.getVisitPrescriptionDetails(orderId, patientId);
    }
}
